package com.movie.modules.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.movie.common.exception.RRException;
import com.movie.common.utils.DateUtils;
import com.movie.common.utils.R;
import com.movie.common.utils.SmsUtil;
import com.movie.modules.api.form.LoginForm;
import com.movie.modules.api.service.ApiLoginService;
import com.movie.modules.jf.entity.VipEntity;
import com.movie.modules.jf.service.VipService;
import com.movie.modules.sys.entity.SysCaptchaEntity;
import com.movie.modules.sys.oauth2.TokenGenerator;
import com.movie.modules.sys.service.SysCaptchaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author JF-PJJ
 */
@Service
@Transactional
public class ApiLoginServiceImpl implements ApiLoginService {

    @Value("${ismsg}")
    private Boolean ismsg;

    @Autowired
    private SysCaptchaService captchaService;
    @Autowired
    private VipService vipService;

    @Override
    public R sendMsg(String mobile,String uuid) {
        if(StringUtils.isBlank(uuid)){
            throw new RRException("uuid不能为空");
        }
        if (ismsg) {
            String code= SmsUtil.randomCode();
            SysCaptchaEntity captchaEntity = new SysCaptchaEntity();
            captchaEntity.setUuid(uuid);
            captchaEntity.setCode(code);
            //5分钟后过期
            captchaEntity.setExpireTime(DateUtils.addDateMinutes(new Date(), 5));
            captchaService.save(captchaEntity);
            return R.ok().put("data", SmsUtil.sendMsg(mobile, code));
        }else {
            return R.ok();
        }
    }

    @Override
    public R login(LoginForm loginForm) {
        if(StringUtils.isEmpty(loginForm.getCode())){
            return R.error("验证码不能为空");
        }
        if(ismsg){
            /*判断验证码是否正确*/
            if(!captchaService.validate(loginForm.getUuid(), loginForm.getCode())){
                return R.error("验证码不正确");
            }
        }
        /*判断当前手机号是否存在*/
        VipEntity vip=vipService.getOne(new QueryWrapper<VipEntity>().eq("phone",loginForm.getPhone()));
        String msg="登录成功";
        if(vip==null){
            vip=new VipEntity();
            vip.setAvatar("avatar.png");
            vip.setNickname(loginForm.getUuid());
            vip.setPhone(loginForm.getPhone());
            vip.setSex(2);
            vip.setJob("未填写");
            vip.setStatus(1);
            vip.setBirthday("2008-08-08");
            vip.setCity("未填写");
            vipService.save(vip);
            msg="注册成功";
        }
        if(vip.getStatus()==1){
            Map<String,Object> map=new HashMap<>();
            map.put("token", TokenGenerator.generateValue());
            map.put("info",msg);
            map.put("vip",vip);
            return R.ok().put("data",map);
        }else{
            return R.error("该账户已经被封禁！");
        }

    }
}
